#!/bin/bash
###############################################################################
# @用例ID: 20231117-194731-457667928
# @用例名称: pigz_test
# @用例级别: 3
# @用例标签:
# @用例类型: 功能
###############################################################################
[ -z "$TST_TS_TOPDIR" ] && {
    TST_TS_TOPDIR="$(realpath "$(dirname "$0")/..")"
    export TST_TS_TOPDIR
}
source "${TST_TS_TOPDIR}/tst_common/lib/common.sh" || exit 1
###############################################################################

g_tmpdir="$(mktemp -d)"

tc_setup() {
    msg "this is tc_setup"
    # @预置条件:安装pigz
    # @预置条件:已安装pigz
    assert_true yum install -y pigz
    rpm -qa | grep pigz > /dev/null 2>&1
    assert_true [ $? -eq 0 ]

    return 0
}

do_test() {
    msg "this is do_test"

    # @测试步骤:1:创建需要压缩的文件
    # @预期结果:1:存在对应文件
    testFile=${g_tmpdir}/test.txt.gz
    testZipFile=${g_tmpdir}/test.txt.zip
    echo "This is test" > ${g_tmpdir}/test.txt

    # @测试步骤:2:创建目录和文件
    # @预期结果:2:目录和文件存在
    mkdir  ${g_tmpdir}/test_dir
    mkdir  ${g_tmpdir}/test_dir/dir_1
    test0File=${g_tmpdir}/test_dir/test0.txt.gz
    test1File=${g_tmpdir}/test_dir/test1.txt.gz
    echo "This is test0" > ${g_tmpdir}/test_dir/test0.txt
    echo "This is test1" > ${g_tmpdir}/test_dir/test1.txt

    # @测试步骤:3:压缩文件保留原始文件
    # @预期结果:3:成功压缩并文件保留
    assert_true pigz -k -f ${g_tmpdir}/test.txt
    assert_true [ -f "${testFile}" ]
    rm -f ${testFile}

    # @测试步骤:4:设置并行进程数量, 保留原始文件, 强制覆盖
    # @预期结果:4:成功压缩并文件保留
    assert_true pigz -p 4 -k -f ${g_tmpdir}/test.txt
    assert_true [ -f "${testFile}" ]
    rm -f ${testFile}

    # @测试步骤:5:压缩成zip格式的文件
    # @预期结果:5:成功压缩并文件保留
    assert_true pigz -K -k ${g_tmpdir}/test.txt
    assert_true [ -f "${testZipFile}" ]
    rm -f ${testZipFile}

    # @测试步骤:6:指定blocksize压缩
    # @预期结果:6:成功压缩并文件保留
    assert_true pigz -k -f -b 256 ${g_tmpdir}/test.txt
    assert_true [ -f "${testFile}" ]
    rm -f ${testFile}
    
    # @测试步骤:7:将压缩内存输出到屏幕
    # @预期结果:7:成功打印压缩内存
    assert_true pigz -c -f ${g_tmpdir}/test.txt

    # @测试步骤:8:递归压缩目录下所有文件
    # @预期结果:8:所有文件压缩成功
    assert_true pigz -p 4 -r -k -f ${g_tmpdir}/test_dir
    assert_true [ -f "${test0File}" ]
    assert_true [ -f "${test1File}" ]
    rm -f ${test0File}
    rm -f ${test1File}

    return 0
}

tc_teardown() {
    msg "this is tc_teardown"
    rm -rfv "$g_tmpdir" || return 1
    return 0
}

###############################################################################
tst_main "$@"
###############################################################################
